package com.intel.two.service.smarthome.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.R;
import com.intel.two.exception.CustomerException;
import com.intel.two.mapper.smarthome.GuaranteeMapper;
import com.intel.two.pojo.smarthome.Guarantee;
import com.intel.two.service.smarthome.GuaranteeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @BelongsProject: intelligentcommunity
 * @BelongsPackage: com.intel.two.service.smarthome.impl
 * @Author: yanhongwei
 * @CreateTime: 2022-10-13  17:07
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class GuaranteeServiceImpl implements GuaranteeService {

    @Autowired
    GuaranteeMapper guaranteeMapper;

    @Override
    public R guaranteeList(Map<String, Object> parameters) {
        Integer limit = Integer.parseInt((String) parameters.get("limit"));
        Integer page = Integer.parseInt((String) parameters.get("page"));
//        开启分页
        PageHelper.startPage(page, limit);
        List<Guarantee> guarantees = guaranteeMapper.guaranteeList(parameters);
        Map<String, Object> map = new HashMap<>();
//        获取分页的相关信息
        PageInfo<Guarantee> pageInfo = new PageInfo<>(guarantees);
        map.put("guarantees", guarantees);//学生信息
        map.put("count", pageInfo.getTotal());//总记录数
        return R.ok().data(map);

    }

    @Override
    public R guaranteeAdd(Guarantee guarantee) {
//        每次添加保修单就默认为购买家居的时间
        guarantee.setGuarantee_buydate(new Date());
        //添加保修单
        int i = guaranteeMapper.guaranteeAdd(guarantee);
        return i > 0 ? R.ok() : R.error();
    }

    @Override
    public R guaranteeDel(List<Guarantee> guarantees) {

        List<Integer> collect = guarantees.stream().map(guarantee -> guarantee.getGuarantee_id()).collect(Collectors.toList());

        int i = guaranteeMapper.guaranteeDel(collect);

        return i > 0 ? R.ok() : R.error();
    }

    @Override
    public R guaranteeEdit(Guarantee guarantee) {

        //添加保修单
        int i = guaranteeMapper.guaranteeEdit(guarantee);
        return i > 0 ? R.ok() : R.error();

    }
}
